<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!--<el-form-item label="用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="所在的城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入所在的城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主营商品介绍" prop="commodity">
        <el-input
          v-model="queryParams.commodity"
          placeholder="请输入主营商品介绍"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌介绍" prop="brandIntroduce">
        <el-input
          v-model="queryParams.brandIntroduce"
          placeholder="请输入品牌介绍"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照注册号" prop="businessLicenseNumber">
        <el-input
          v-model="queryParams.businessLicenseNumber"
          placeholder="请输入营业执照注册号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法定代表人" prop="legalRepresentative">
        <el-input
          v-model="queryParams.legalRepresentative"
          placeholder="请输入法定代表人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照所在地" prop="businessLicenseAddress">
        <el-input
          v-model="queryParams.businessLicenseAddress"
          placeholder="请输入营业执照所在地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业注册资金" prop="registeredCapital">
        <el-input
          v-model="queryParams.registeredCapital"
          placeholder="请输入企业注册资金"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照有效期" prop="businessLicenseTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.businessLicenseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择营业执照有效期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress">
        <el-input
          v-model="queryParams.companyAddress"
          placeholder="请输入公司地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照经营范围" prop="businessScope">
        <el-input
          v-model="queryParams.businessScope"
          placeholder="请输入营业执照经营范围"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:supplier:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:supplier:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" class="table-expand" label-width="150px">
            <el-form-item label="企业名称">
              <span>{{ scope.row.enterpriseName }}</span>
            </el-form-item>
            <el-form-item label="品牌介绍">
              <span>{{ scope.row.brandIntroduce }}</span>
            </el-form-item>
            <el-form-item label="营业执照注册号">
              <span>{{ scope.row.businessLicenseNumber }}</span>
            </el-form-item>
            <el-form-item label="法定代表人">
              <span>{{ scope.row.legalRepresentative }}</span>
            </el-form-item>
            <el-form-item label="营业执照所在地">
              <span>{{ scope.row.businessLicenseAddress }}</span>
            </el-form-item>
            <el-form-item label="企业注册资金">
              <span>{{ scope.row.registeredCapital }}</span>
            </el-form-item>
            <el-form-item label="营业执照有效期">
              <span>{{ parseTime(scope.row.businessLicenseTime, '{y}-{m}-{d}') }}</span>
            </el-form-item>
            <el-form-item label="公司地址">
              <span>{{ scope.row.companyAddress }}</span>
            </el-form-item>
            <el-form-item label="营业执照经营范围">
              <span>{{ scope.row.businessScope }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <!--<el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="supplierId" />-->
      <el-table-column label="用户" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="所在的城市" align="center" prop="city" />
      <el-table-column label="主营商品介绍" align="center" prop="commodity" />
      <!--<el-table-column label="品牌介绍" align="center" prop="brandIntroduce" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="营业执照注册号" align="center" prop="businessLicenseNumber" />
      <el-table-column label="法定代表人" align="center" prop="legalRepresentative" />
      <el-table-column label="营业执照所在地" align="center" prop="businessLicenseAddress" />
      <el-table-column label="企业注册资金" align="center" prop="registeredCapital" />
      <el-table-column label="营业执照有效期" align="center" prop="businessLicenseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.businessLicenseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司地址" align="center" prop="companyAddress" />
      <el-table-column label="营业执照经营范围" align="center" prop="businessScope" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:supplier:edit']"
          >审批</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <!--<el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="所在的城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入所在的城市" />
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="主营商品介绍" prop="commodity">
          <el-input v-model="form.commodity" placeholder="请输入主营商品介绍" />
        </el-form-item>
        <el-form-item label="品牌介绍" prop="brandIntroduce">
          <el-input v-model="form.brandIntroduce" type="textarea" placeholder="请输入品牌介绍" />
        </el-form-item>
        <el-form-item label="营业执照注册号" prop="businessLicenseNumber">
          <el-input v-model="form.businessLicenseNumber" placeholder="请输入营业执照注册号" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalRepresentative">
          <el-input v-model="form.legalRepresentative" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="营业执照所在地" prop="businessLicenseAddress">
          <el-input v-model="form.businessLicenseAddress" placeholder="请输入营业执照所在地" />
        </el-form-item>
        <el-form-item label="企业注册资金" prop="registeredCapital">
          <el-input v-model="form.registeredCapital" placeholder="请输入企业注册资金" />
        </el-form-item>
        <el-form-item label="营业执照有效期" prop="businessLicenseTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.businessLicenseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择营业执照有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="营业执照经营范围" prop="businessScope">
          <el-input v-model="form.businessScope" placeholder="请输入营业执照经营范围" />
        </el-form-item>-->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注，或申请失败原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, exportSupplier } from "@/api/module/supplier";

export default {
  name: "Supplier",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        name: undefined,
        mobile: undefined,
        city: undefined,
        commodity: undefined,
        brandIntroduce: undefined,
        enterpriseName: undefined,
        businessLicenseNumber: undefined,
        legalRepresentative: undefined,
        businessLicenseAddress: undefined,
        registeredCapital: undefined,
        businessLicenseTime: undefined,
        companyAddress: undefined,
        businessScope: undefined,
        remark: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        commodity: [
          { required: true, message: "主营商品介绍不能为空", trigger: "blur" }
        ],
        brandIntroduce: [
          { required: true, message: "品牌介绍不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTDictDatas("module_supplier_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询供应商列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: undefined,
        userId: undefined,
        name: undefined,
        mobile: undefined,
        city: undefined,
        commodity: undefined,
        brandIntroduce: undefined,
        enterpriseName: undefined,
        businessLicenseNumber: undefined,
        legalRepresentative: undefined,
        businessLicenseAddress: undefined,
        registeredCapital: undefined,
        businessLicenseTime: undefined,
        companyAddress: undefined,
        businessScope: undefined,
        remark: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids
      getSupplier(supplierId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审批";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supplierId != undefined) {
            updateSupplier(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSupplier(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$confirm('是否确认删除供应商编号为"' + supplierIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplier(supplierIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有供应商数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSupplier(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
